package com.example.leaderapplication.Base;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.leaderapplication.Utils.SPUtil;
import com.example.leaderapplication.Utils.StringUtil;
import com.example.leaderapplication.Utils.ToastUtil;
import com.example.leaderapplication.View.Activity.LoginActivity;
import com.example.leaderapplication.Widgets.LoadingDialog;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseActivity extends AppCompatActivity {

    public String baseUrl;
    private LoadingDialog.Builder loadBuilder;
    private LoadingDialog dialog;
    public Map<String, String> headers;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题
        makeStatusBarTransparent(this);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(setLayoutId());

        //获取headmap
        if (headers == null) {
            headers = new HashMap<>();
        }
        String token = (String) SPUtil.get(this,  BaseConfig.ACCESS_TOKEN, "");
        if (!StringUtil.isNullOrEmpty(token)) {
            headers.put("Token", token);
        }
        // 获取基础地址

        baseUrl = (String) SPUtil.get(BaseActivity.this, BaseConfig.SAVE_BASE_URL, "");
        if (StringUtil.isNullOrEmpty(baseUrl)) {
            baseUrl = BaseConfig.Base_URL;
        }



        initView();
        doSomeThing();

    }

    public abstract int setLayoutId();

    protected abstract void initView();

    protected abstract void doSomeThing();


    public static void makeStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    // 显示加载对话框
    public void showLoadDialog() {
        loadBuilder = new LoadingDialog.Builder(this)
                .setMessage("加载中...")
                .setCancelable(true)
                .setCancelOutside(true);
        dialog = loadBuilder.create();
        dialog.show();

    }

    public void dimessDialog() {
        LoadingDialog.getInstence(this).dimessDialog();
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
////                    startActivity(new Intent(getContext(), LoginActivity.class));
////                    getActivity().finish();
////                    System.exit(0);
//            return true;
//        }
//        return false;
//    }

}
